package com.shen.accountbook.module.home.sortmanagefragment;

import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.shen.accountbook.R;
import com.shen.accountbook.bean.temp.FirstSortBean;
import com.shen.accountbook.bean.temp.SecondSortBean;
import com.shen.accountbook.bean.temp.ThirdSortBean;
import com.shen.accountbook.config.Constant;
import com.shen.accountbook.module.base.MVPFragment;
import com.shen.accountbook.utils.StringUtils;
import com.shen.accountbook.widget.AddSortDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author:  shen
 * date:    2018/9/7
 */
public class SortManageFragment extends MVPFragment<SortManagePresenterImpl> implements SortManageView {



    @BindView(R.id.tv_first_sort_name_sortmanagefragment)
    TextView mTvFirstSortName;
    @BindView(R.id.tv_second_sort_name_sortmanagefragment)
    TextView mTvSecondSortName;
    @BindView(R.id.tv_third_sort_name_sortmanagefragment)
    TextView mTvThirdSortName;

    @BindView(R.id.rv_first_sortmanagefragment)
    RecyclerView mRvFirst;
    @BindView(R.id.rv_second_sortmanagefragment)
    RecyclerView mRvSecond;
    @BindView(R.id.rv_third_sortmanagefragment)
    RecyclerView mRvThird;


    FirstSortBean mFirstSortBean;
    SecondSortBean mSecondSortBean;
    ThirdSortBean mThirdSortBean;

    SortManageAdapter<FirstSortBean> mFirstAdapter;
    SortManageAdapter<SecondSortBean> mSecondAdapter;
    SortManageAdapter<ThirdSortBean> mThirdAdapter;

    @Override
    protected SortManagePresenterImpl createPresent(Context context) {
        return new SortManagePresenterImpl(context, this);
    }

    @Override
    public int getResId() {
        return R.layout.fragment_sort_manage;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {
        initRv();
    }


    private void initRv() {
        mRvFirst.setLayoutManager(mPresenter.getLayoutManager());
        mRvSecond.setLayoutManager(mPresenter.getLayoutManager());
        mRvThird.setLayoutManager(mPresenter.getLayoutManager());

        // 设置ItemDecoration
        setFirstAdapter();  // 适配器
    }


    /**
     * 点击"一级标题"触发的事件
     */
    private void setFirstAdapter() {

        if(mFirstAdapter == null){
            mFirstAdapter = mPresenter.setSortAdapter((mFirstSortBean = null));
            mRvFirst.setAdapter(mFirstAdapter);
        }else {
            mFirstAdapter.setBeanList(mPresenter.getFirstSortList());
        }

        if(mFirstAdapter.getBeanList() != null && mFirstAdapter.getBeanList().size() > 0) {
            setSecondAdapter(mFirstAdapter.getBeanList().get(0));
        }else {
            mSecondAdapter.clear();
            mThirdAdapter.clear();
        }
    }

    /**
     * 点击"二级标题"触发的事件
     */
    private void setSecondAdapter(FirstSortBean bean) {
        mFirstSortBean = bean;

        if(mSecondAdapter == null){
            mSecondAdapter = mPresenter.setSortAdapter(bean);
            mRvSecond.setAdapter(mSecondAdapter);
        }else {
            mSecondAdapter.setBeanList(mPresenter.getSecondSortList(bean));
        }

        if(mSecondAdapter.getBeanList() != null && mSecondAdapter.getBeanList().size() > 0) {
            setThirdAdapter(mSecondAdapter.getBeanList().get(0));
        }else {
            mThirdAdapter.clear();
        }
    }

    /**
     * 点击"三级标题"触发的事件
     */
    private void setThirdAdapter(SecondSortBean bean) {
        mSecondSortBean = bean;

        if(mThirdAdapter == null){
            mThirdAdapter = mPresenter.setSortAdapter(bean);
            mRvThird.setAdapter(mThirdAdapter);
        }else {
            mThirdAdapter.setBeanList(mPresenter.getThirdSortList(bean));
        }
    }


    @OnClick({R.id.tv_first_sort_name_sortmanagefragment,
            R.id.tv_second_sort_name_sortmanagefragment,
            R.id.tv_third_sort_name_sortmanagefragment})
    public void onViewClicked(View view) {

        switch (view.getId()){
            case R.id.tv_first_sort_name_sortmanagefragment:
                showAddFirstDialog();
                break;

            case R.id.tv_second_sort_name_sortmanagefragment:
                showAddSecondDialog();
                break;

            case R.id.tv_third_sort_name_sortmanagefragment:
                showAddThirdDialog();
                break;
        }
    }

    /* 显示添加的窗口 */
    private void showAddFirstDialog(){
        AddSortDialog dialog = new AddSortDialog(mContext, null);
        dialogSetCallBack(dialog);
        dialog.show();
    }

    private void showAddSecondDialog(){
        if(mFirstSortBean == null)
            return;

        AddSortDialog dialog = new AddSortDialog(mContext, mFirstSortBean);
        dialogSetCallBack(dialog);
        dialog.show();
    }

    private void showAddThirdDialog(){
        if(mSecondSortBean == null)
            return;

        AddSortDialog dialog = new AddSortDialog(mContext, mSecondSortBean);
        dialogSetCallBack(dialog);
        dialog.show();
    }


    private void dialogSetCallBack(AddSortDialog dialog){
        if(dialog != null){
            dialog.setCallBack(new AddSortDialog.CallBack() {
                @Override
                public void onClickLeftBtn() {

                }

                @Override
                public <K> void onClickRightBtn(K bean, String name) {
                    if(bean == null){
                        mPresenter.insertFirstSort(name);
                    }else if(bean instanceof FirstSortBean){
                        mPresenter.insertSecondSort((FirstSortBean)bean, name);
                    }else if(bean instanceof SecondSortBean){
                        mPresenter.insertThirdSort((SecondSortBean) bean, name);
                    }else if(bean instanceof ThirdSortBean){

                    }
                }
            });
        }
    }

    /* 添加后刷新列表 */
    @Override
    public <T> void dataSetChanged(T bean) {
        if(bean == null){
            mFirstAdapter.setBeanList(mPresenter.getFirstSortList());
            mFirstAdapter.notifyDataSetChanged();

        }else if(bean instanceof FirstSortBean){
            mSecondAdapter.setBeanList(mPresenter.getSecondSortList((FirstSortBean)bean));
            mSecondAdapter.notifyDataSetChanged();

        }else if(bean instanceof SecondSortBean){
            mThirdAdapter.setBeanList(mPresenter.getThirdSortList((SecondSortBean)bean));
            mThirdAdapter.notifyDataSetChanged();

        }else if(bean instanceof ThirdSortBean){

        }

    }


    /* 列表项的点击事件 */
    @Override
    public void setFirstItemOnClick(FirstSortBean firstBean) {
        setSecondAdapter(firstBean);
    }

    @Override
    public void setSecondItemOnClick(SecondSortBean secondBean) {
        setThirdAdapter(secondBean);
    }

    @Override
    public void setThirdItemOnClick(ThirdSortBean thirdSortBean) {
        mThirdSortBean = thirdSortBean;

    }

}
